package view;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PrinterException;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import view.DBConnect;
import view.MetaReportDAO;
import view.TopComplaintReportDAO;
import view.StateComplaintDAO;

public class ShowIDE implements ActionListener {

	private static JFrame frame = new JFrame("Team 4 Reports");
	private static JFrame stateframe = null;
	private static JTextArea ReportOutput = new JTextArea();
	private static JButton Report1 = new JButton();// Josh
	private static JButton Report2 = new JButton();// Jeff
	private static JButton Report3 = new JButton();// David
	private static JButton MetaReport = new JButton();
	private static JButton Save = new JButton();
	private static JButton Print = new JButton();
	private static JButton State = new JButton();	//David
    private static JTextField Statename = new JTextField();
    DBConnect conn = null;

    public ShowIDE(String[] ConnectionVars)
    {
    	String Host = null;
		String Port = null;
		String Database = null;
		String User = null;
		String Password = null;
		String Schema = null;
    	try
    	{
    		//database host address
    		Host = ConnectionVars[0];
    		//port to contact
    		Port = ConnectionVars[1];
    		//database name
    		Database = ConnectionVars[2];
    		//user name
    		User = ConnectionVars[3];
    		//user password
    		Password = ConnectionVars[4];
    		//Schema corresponds to table prefixes ex:TEAM4.CUST_COMPLAINT where TEAM4 is the schema
    		Schema = ConnectionVars[5];
    	}
    	catch(Exception e)
    	{
    		//backup-default config - set for lab - Jeff
    		Host = "10.37.8.17";
    		Port = "1521";
    		Database = "CSC342";
    		User = "Team4";
    		Password = "Zeus438";
    		Schema = "CSC342";
    		controller.ErrorHandle.LogError(e);
    	}
    	//DO NOT REMOVE, this will be used for executing the program once we have final build
    	conn = new DBConnect(Host, Port, Database, User, Password, Schema);
    }
    
    
	public void CreateWindows() {
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JPanel functionpanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JPanel contentpanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

		// Extra Functionality
		functionpanel.setPreferredSize(new Dimension(100, 600));
		Save.setText(" Save ");
		Print.setText(" Print ");
		Save.setToolTipText("Save Report To Disk");
		Print.setToolTipText("Print Report");
		Save.addActionListener(this);
		Print.addActionListener(this);
		functionpanel.add(Save);
		functionpanel.add(Print);
		frame.getContentPane().add(functionpanel, BorderLayout.LINE_START);
		// End Extra Functionality

		// Report Buttons
		// panel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		contentpanel.setPreferredSize(new Dimension(600, 100));
		Report1.setText(" Report 1 ");
		Report2.setText(" Report 2 ");
		Report3.setText(" Report 3 ");
		MetaReport.setText(" Meta Data Report ");
		Report1.setToolTipText("Who are the top 5 customers for customer complaints? How many complaints do each have?");
		Report2.setToolTipText("Of the complaints we received last year, what products were involved, their price, their material price and the percentage of complaints vs units sold.");
		Report3.setToolTipText("For this State which month had the most complaints?");
		MetaReport.setToolTipText("What driver is the Oracle DB using? What are TEAM4's critical tables?");
		Report1.addActionListener(this);
		Report2.addActionListener(this);
		Report3.addActionListener(this);
		MetaReport.addActionListener(this);
		contentpanel.add(Report1);
		contentpanel.add(Report2);
		contentpanel.add(Report3);
		contentpanel.add(MetaReport);
		frame.getContentPane().add(contentpanel, BorderLayout.CENTER);
		// End Report Buttons

		// Report Output
		ReportOutput.setPreferredSize(new Dimension(600, 600));
		contentpanel.setPreferredSize(ReportOutput.getPreferredSize());
		contentpanel.add(ReportOutput);
		frame.getContentPane().add(contentpanel, BorderLayout.CENTER);
		// End Report Output

		frame.pack(); // sets windows size to contain the contents
		frame.setVisible(true);
	}
	public void ShowStateFrame()
    {
            stateframe = new JFrame("Enter a State");
            stateframe.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            JPanel panel = new JPanel(new FlowLayout(FlowLayout.LEFT));
           
            Statename.setPreferredSize( new Dimension( 150, 20 ) );
            State.setText(" OK ");
            State.addActionListener(this);
            panel.add(Statename);
            panel.add(State);
            stateframe.add(panel);
            stateframe.pack(); //sets windows size to contain the contents
            stateframe.setVisible(true);
    }

	public void actionPerformed(ActionEvent evt) {
		Object src = evt.getSource();
		String strReturnedText = "";
				
		if (src == Report1) {
			
			System.out.println("Running query, please wait. Average query run time is around 16 seconds. \n");
			TopComplaintReportDAO thisTop = null;
			thisTop = new TopComplaintReportDAO(conn);
			strReturnedText = thisTop.ViewTopComplaintReport();
			ReportOutput.setText(strReturnedText);

		} else if (src == Report2) {
			
            ReportOutput.setText(null);
            ReportOutput.setText(new view.ComplaintCostReportDAO(conn).GetReportString());

		} else if (src == Report3) {
			  
			ShowStateFrame();
		}
		  else if (src == State){
			  
			StateComplaintDAO thisStateComplaint = null;
			thisStateComplaint = new StateComplaintDAO(conn);
			strReturnedText = thisStateComplaint.ViewStateComplaint(Statename.getText());
			ReportOutput.setText(strReturnedText);
			stateframe.dispose();
			
		}
		  else if (src == MetaReport) {
			
			MetaReportDAO thisMeta = null;
			thisMeta = new MetaReportDAO(conn);
			strReturnedText = thisMeta.ViewOracleDriver();
			strReturnedText += thisMeta.ViewCriticalTable();
			ReportOutput.setText(strReturnedText);
			
		} else if (src == Save) {
			
			saveReport();
			
		} else if (src == Print) {
			
			printReport();
		}
	}

	private void saveReport() {
		File file;
		BufferedWriter writer = null;
		try {
			JFileChooser fc = new JFileChooser();
			int returnVal = fc.showOpenDialog(frame);//allows file selection

			if (returnVal == JFileChooser.APPROVE_OPTION) {
				file = fc.getSelectedFile();//get file when selected
			} else {
				return;//exit if none
			}
			writer = new BufferedWriter(new FileWriter(file));
			writer.write(ReportOutput.getText()); //write report to file
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				writer.close();//save and close no matter what
			} catch (Exception e) {
			}
		}
	}

	private void printReport() {
		try {
			boolean complete = ReportOutput.print();
			if (complete) {
				System.out.println("Report Printed");
			} else {
				System.out.println("Print Cancelled");
			}
		} catch (PrinterException pe) {
			System.out.println("Print Error\n" + pe.getMessage());
		}
	}

}

